package com.atguigu.atcrowdfunding.manager.controller;

import com.atguigu.atcrowdfunding.bean.Permission;
import com.atguigu.atcrowdfunding.manager.service.PermissionService;
import com.atguigu.atcrowdfunding.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xzt
 * @create 2021-02-23 9:27
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/index")
    public String index(){
        return "permission/index";
    }

    //Demo3 - 采用一次性获取数据库所有数据，只对数据库做一次查询
    @ResponseBody
    @RequestMapping("/loadData")
    public Object loadData(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Permission> root = new ArrayList<>();

            List<Permission> childredPermissions = permissionService.queryAllPermission();

            HashMap<Integer, Permission> map = new HashMap<>();

            for (Permission innerpermission : childredPermissions) {
                map.put(innerpermission.getId(),innerpermission);
            }

            for (Permission permission : childredPermissions) {
                Permission child = permission;

                if(child.getPid() == null){
                    root.add(child);
                }else{
                    Permission parent = map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }

            ajaxResult.setData(root);
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("加载许可树数据成功");
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("加载许可树数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    //Demo2 - 从数据库表查询数据
    /*@ResponseBody
    @RequestMapping("/loadData")
    public Object loadData(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Permission> root = new ArrayList<>();

            //父
            Permission permission = permissionService.getRootPermission();

           *//* //子
            List<Permission> children = permissionService.getChildrenPermissionByPid(permission.getId());

            //设置父子关系
            permission.setChildren(children);

            for (Permission child : children) {
                child.setOpen(true);
                List<Permission> innerChildren = permissionService.getChildrenPermissionByPid(child.getId());
                child.setChildren(innerChildren);
            }*//*

            //使用递归
            queryChildPermissions(permission);

            root.add(permission);
            ajaxResult.setData(root);
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("加载许可树数据成功");
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("加载许可树数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }*/

    private void queryChildPermissions(Permission root){
        List<Permission> childrenPermission = permissionService.getChildrenPermissionByPid(root.getId());

        root.setChildren(childrenPermission);

        for (Permission permission : childrenPermission) {
            queryChildPermissions(permission);
        }

    }

    //Demo1 - 模拟数据生成树
    /*@ResponseBody
    @RequestMapping("/loadData")
    public Object loadData(){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            List<Permission> root = new ArrayList<>();

            //父
            Permission permission = new Permission();
            permission.setName("系统权限菜单");
            permission.setOpen(true);
            //子
            List<Permission> children = new ArrayList<>();

            Permission permission1 = new Permission();
            permission1.setName("控制面板");
            Permission permission2 = new Permission();
            permission2.setName("权限管理");

            children.add(permission1);
            children.add(permission2);

            //设置子关系
            permission.setChildren(children);

            root.add(permission);

            ajaxResult.setData(root);

            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("加载许可树数据成功");
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("加载许可树数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }*/

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "permission/add";
    }

    @ResponseBody
    @RequestMapping("/doAdd")
    public Object doAdd(Permission permission){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            int count = permissionService.savePermission(permission);
            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("添加许可数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("添加许可数据失败");
            }

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("添加许可数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model){

        Permission permission = permissionService.getChildrenPermissionById(id);

        model.addAttribute("permission",permission);

        return "permission/update";
    }


    @ResponseBody
    @RequestMapping("/doUpdate")
    public Object doUpdate(Permission permission){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            int count = permissionService.UpdatePermission(permission);
            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("修改许可数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("修改许可数据失败");
            }

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("修改许可数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/doDelete")
    public Object doDelete(Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            int count = permissionService.deletePermissionById(id);
            if(count == 1){
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("删除许可数据成功");
            }else{
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("删除许可数据失败");
            }

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除许可数据失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
